package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.activitys.AppContext;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.view.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.base.BaseFragment;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.PageScroller;
import zuo.biao.library.ui.TopMenuWindow;

public class MapOnlineListFragment extends BaseFragment {
    private final String TAG = "MapOnlineListFragment";

    //与Activity通信<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 创建一个Fragment实例
     *
     * @return
     */
    public static MapOnlineListFragment createInstance() {
        return new MapOnlineListFragment();
    }
    //与Activity通信>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Handler mFHandler = null;// for 双击全屏显示

    public void setFatherHandler(Handler fHandler) {
        mFHandler = fHandler;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.maponlinelist_fragment);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

        return view;//返回值必须为view
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ListView lv_online;

    @Override
    public void initView() {//必须在onCreateView方法内调用
        lv_online = (ListView) findViewById(R.id.lv_online);
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    private DemoAdapter adapter;

    private void setList(List<Entry<Integer, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_online.setAdapter(adapter);
        }
        adapter.refresh(list);
    }

    private List<Entry<Integer, String>> maplist;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        String[] MAPS_NAME = getResources().getStringArray(R.array.maps_online);

        maplist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < MAPS_NAME.length; i++) {
            maplist.add(new Entry<Integer, String>(i, MAPS_NAME[i]));
        }
        setList(maplist);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lv_online.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onMapInfo(position);
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_online).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    void onMapInfo(int position) {
        SysUtils.getInstance().mapsource = position;
        SysUtils.getInstance().saveMapSource();
        adapter.notifyDataSetChanged();
    }

    private final int REQUEST_TO_TOP_MENU = 30;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }

    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.mapitem_view, parent, false);
            }

            TextView iv_check = BaseViewHolder.get(convertView, R.id.iv_check);
            TextView tv_mapname = BaseViewHolder.get(convertView, R.id.tv_mapname);
            iv_check.setTypeface(AppContext.getInstances().getIconfont());

            final Entry<Integer, String> data = getItem(position);
            int index = data.getKey();
            tv_mapname.setText(data.getValue());

            if (SysUtils.getInstance().mapsource == position) {
                iv_check.setVisibility(View.VISIBLE);
            } else {
                iv_check.setVisibility(View.GONE);
            }

            return super.getView(position, convertView, parent);
        }
    }
}